<?php
/**
 *
 *通知公告控制器
 *
 **/
namespace Home\Controller;

use Vendor\Page;

class NoticeController extends ComController
{

    /*
     * 公告列表
     */
    public  function index(){

        $sc_id = I('uid') ? I('uid') : 0;
        $p= intval($_GET['p'])?$_GET['p']:1;
        $notice = M('notice');
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $data=$notice->where('sc_id ='.$sc_id )
				        ->field('id,title,intro,picture,add_time,content')
				        ->order('add_time desc')
				        ->limit($offset.','.$pagesize)
				        ->select();
        if(!empty($data)){
        	foreach ($data as $k=>$v){
        		if(empty($v['picture'])){
        			//如果图片为空，则从文章中读取第一张图片
        			$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
        			preg_match($pattern,$v['content'],$match);
        			if(!empty($match)){
        				$data[$k]['picture']=$match[1];
        				unset($data[$k]['content']);
        			}else{
        				//如果正文中也没有图片，则拿幼儿园图片
        				$pic=M('member')->where("uid=$sc_id")->field('head')->find();  
        				$data[$k]['picture']=$pic['head'];   
        				unset($data[$k]['content']);
        			}
        		}
        	}
        }
        $this->assign('data',$data);
        $this->display('notification');
    }
//获取更多公告
public function getData(){

	$p= intval($_GET['p'])?$_GET['p']:1;
	$sc_id = I('get.uid');
	$pagesize = 10;#每页数量
	$offset = $pagesize * ($p - 1);//计算记录偏移量
	$notice = M('notice');
$data=$notice->where('sc_id ='.$sc_id )
				        ->field('id,title,intro,picture,add_time,content')
				        ->order('add_time desc')
				        ->limit($offset.','.$pagesize)
				        ->select();
        if(!empty($data)){
        	foreach ($data as $k=>$v){
        		if(empty($v['picture'])){
        			//如果图片为空，则从文章中读取第一张图片
        			$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";
        			preg_match($pattern,$v['content'],$match);
        			if(!empty($match)){
        				$data[$k]['picture']=$match[1];
        				unset($data[$k]['content']);
        			}else{
        				//如果正文中也没有图片，则拿幼儿园图片
        				$pic=M('member')->where("uid=$sc_id")->field('head')->find();  
        				$data[$k]['picture']=$pic['head'];   
        				unset($data[$k]['content']);
        			}
        		}
        	}
        }else{
        	echo 1;die;
        }

	$this->assign('data',$data);
	$this->display();
}

    /*
     * 公告详情
     */
     public  function detail(){
         $id=I('id') ? I('id') : 0  ;
         $notice = M('notice');
         $data=$notice->where('id ='.$id)->find();
         $this->assign('data',$data);
         $this->display('notification-detail');
  }








}